------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    不朽尸王 脱离后入睡.

]]--
------------------------------------------------------------------------------------------------------------------------------------------------
---
    
------------------------------------------------------------------------------------------------------------------------------------------------
--- 素材
    local assets =
    {

    }
------------------------------------------------------------------------------------------------------------------------------------------------
--- debuff 激活
    local function OnAttached(inst,player) -- 玩家得到 debuff 的瞬间。 穿越洞穴、重新进存档 也会执行。
        inst.entity:SetParent(player.entity)
        inst.Transform:SetPosition(0,0,0)
        -----------------------------------------------------
        --- 计时器
            inst:ListenForEvent("SetTime",function(inst,time)
                inst.time = time
            end)
            inst:DoPeriodicTask(1,function()
                inst.time = (inst.time or 0) - (TUNING.HUTAO_DEBUGGING_MODE and 4 or 1)
                print("info 剩余睡醒时间",inst.time)
                if inst.time <= 0 then
                    inst:Remove()
                    -- player.components.health:DoDelta(1)
                    -- player.components.health:DoDelta(-1)
                    if player.sg and player.sg:HasStateTag("knockout") then
                        player.sg:GoToState("wakeup")
                    end
                    return
                end
                if player.sg and not player.sg:HasStateTag("knockout") then
                    inst:Remove()
                end
            end)
        -----------------------------------------------------
        --- 添加后续DEBUFF
            inst:ListenForEvent("onremove",function(inst)
                local new_debuff_prefab = "hutao_debuff_undying_lich_after_wakeup"                
                local test_num = 100
                while test_num > 0 do
                    local debuff_inst = player:GetDebuff(new_debuff_prefab)
                    if debuff_inst and debuff_inst:IsValid() then
                        break
                    end
                    player:AddDebuff(new_debuff_prefab,new_debuff_prefab)
                    test_num = test_num - 1
                end
            end)
        -----------------------------------------------------
        -- 死亡清除
            inst:ListenForEvent("death",function()
                inst:Remove()
            end,player)
        -----------------------------------------------------
    end
------------------------------------------------------------------------------------------------------------------------------------------------
--- 实体
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddNetwork()
        inst.entity:AddTransform()

        inst:AddTag("CLASSIFIED")

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        -- inst:AddComponent("hutao_data")
        inst:AddComponent("debuff")
        inst.components.debuff:SetAttachedFn(OnAttached)
        inst.components.debuff.keepondespawn = true -- 是否保持debuff 到下次登陆

        return inst
    end
------------------------------------------------------------------------------------------------------------------------------------------------

return Prefab("hutao_debuff_undying_lich_sleep", fn,assets)
